<template>
  <div class="todo-item">
    <input type="text" :value="title" v-if="edit" v-focus @blur="edit=false" @keyup.enter="onUpdate"/>
    <div class="view" v-else>
      <my-checkbox :value="done" @change="$emit('update:todo', {...$props, done: $event})"/>
      <span class="text" @dblclick="edit = true" :class="{ done }">{{title}}</span>
      <button class="destroy" @click="$emit('delete:todo', $props)"></button>
    </div>
  </div>
</template>

<script>
import MyCheckbox from "@/components/MyCheckbox.vue";

export default {
  components: { MyCheckbox },
  name: "TodoItem",
  props: {
    id: {
      type: Number,
      required: true
    },
    title: {
      type: String,
      required: true
    },
    done: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      edit: false
    }
  },
  methods: {
    onUpdate(e) {
      this.$emit('update:todo', {...this.$props, title: e.target.value})
      this.edit = false
    }
  }
};
</script>

<style lang="scss" scoped>
.todo-item {
  position: relative;

  font-size: 24px;
  border-bottom: 1px solid #ededed;

  &:hover {
    .destroy {
      display: block;
    }
  }

    .done {
      text-decoration: line-through;
      color: #ddd;
    }

  .destroy {
    display: none;
    width: 40px;
    height: 40px;
    margin: auto 0;
    font-size: 30px;
    color: #cc9a9a;
    transition: color 0.2s ease-out;
    background: none;
    border: none;
    cursor: pointer;

    &:after {
      content: "×";
    }
  }

  input {
    padding: 8px 16px 8px 60px;
    border: none;
    background: rgba(0, 0, 0, 0.003);
    box-shadow: inset 0  0 10px rgba(175, 47, 47, 0.35);
    color: #888;
    position: relative;
    margin: 0;
    width: 100%;
    font-size: 24px;
    font-family: inherit;
    font-weight: 400;
    line-height: 1.4em;
    outline: none;
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }
}

.view {
  position: relative;
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-left: 15px;

  .text {
    height: 50px;
    line-height: 50px;
    user-select: none;
    flex: 1;
  }
}
</style>
